/**
 * Created by zqk on 2018/4/24.
 */
var currentProjectId = 0;
var projectList;
var currentProjectGrade = 0;
$(document).ready(function () {
    projectList= $('#projectList');
    $('.xh-datagrid-toolbar').parseToolbar();
    //设置禁用
    $('#bt_list_edit').setButtonDisable('eu');
    $('#bt_list_delete').setButtonDisable('eu');
    $('#bt_list_check').setButtonDisable('eu');
    $('#bt_list_archive').setButtonDisable('eu');
    function progressFormatter(value,rowData,rowIndex){
        htmlstr='<div  class="easyui-progressbar progressbar"  style="width: 150px; height: 22px;">'
            +'<div class="progressbar-text" style="width: 150px; height: 20px; line-height: 20px;">'+value+'%</div> '
            +'<div class="progressbar-value" style="width:' +value+'%; height: 20px; line-height: 20px;">'
            +'<div class="progressbar-text" style="width: 150px; height: 20px; line-height: 20px;">'+value+'%</div>'
            +'</div>';
        return htmlstr;
    }
    $btn_menu=$('#bt_list_state').menubutton({
        iconCls: 'fa fa-bars',
        menu: '',
    });
    projectList.treegrid({
        url: moduleUrl+'project/getProject',
        method: 'post',
        rownumbers: true,
        pagination: true,
        pageSize: 50,
        pageList: [20,50,100,200],
        idField: 'iProjectId',
        treeField: 'sProjectName',
        singleSelect:true,
        columns:[[
            {field:'ck',checkbox:true},
            {field:'iProjectId',title:'iProjectId',hidden:true},
            {field:'iProjectGrade',title:'iProjectGrade',hidden:true},
            {field:'sProjectName',title:'项目名称',width:150,align:'left',halign:'center'},
            {field:'sProjectIntroduce',title:'项目简介',width:200,align:'left',halign:'center'},
            {field:'iProjectStatus',title:'项目状态',width:100,align:'center',halign:'center',
                formatter: function(value,row,index){
                    if(value =='1'){
                        var str = '<div style = "background-color:#CCCCCC;color: #ffffff;height: 20px; line-height: 20px;">'+projectStatus[value]+'</div>'
                        return str;
                    }else if(value =='2'){
                        var str = '<div style = "background-color:#2baab1;color: #ffffff;height: 20px; line-height: 20px;">'+projectStatus[value]+'</div>'
                        return str;
                    }else if(value =='3'){
                        var str = '<div style = "background-color:#0079c8;color: #ffffff;height: 20px; line-height: 20px;">'+projectStatus[value]+'</div>'
                        return str;
                    }else if(value =='4'){
                        var str = '<div style = "background-color:#0031ff;color: #ffffff;height: 20px; line-height: 20px;">'+projectStatus[value]+'</div>'
                        return str;
                    }else if(value =='5'){
                        var str = '<div style = "background-color:green;color: #ffffff;height: 20px; line-height: 20px;">'+projectStatus[value]+'</div>'
                        return str;
                    }else if(value =='6'){
                        var str = '<div style = "background-color:red;color: #ffffff;height: 20px; line-height: 20px;">'+projectStatus[value]+'</div>'
                        return str;
                    }

                }
            },
            {field:'iProcess',title:'项目进度', width:160,halign:'center',
                formatter: progressFormatter
            },
            {field:'tCreateDate',title:'创建时间',width:200,align:'center',halign:'center'},
            {field:'tLastModifyDate',title:'最后编辑时间',width:200,align:'center',halign:'center'},
        ]],
        onSelect:function (row) {
            currentProjectId = row['iProjectId'];
            projectList.treegrid('expand',currentProjectId);
            currentProjectGrade = row['iProjectGrade'];
            $('#bt_list_add').setButtonDisable('eu');//项目添加disable
            $('#bt_list_edit').setButtonEnable('eu');//编辑enable
            $('#bt_list_delete').setButtonEnable('eu');//删除enable
            $('#bt_list_check').setButtonEnable('eu');
            $('#bt_list_archive').setButtonEnable('eu');
            if(currentProjectGrade ==1){//选择项目
                $btn_menu = $('#bt_list_state').menubutton({
                    iconCls: 'fa fa-bars',
                    menu: '#projectstatus',
                });
            }
            if($btn_menu.length){
                $($btn_menu.menubutton('options').menu).menu({
                    onClick: function (item) {
                        //item 的相关属性参见API中的menu
                        var status
                        if(item.text == '开发'){
                            status = 2;
                        }else if(item.text == '测试'){
                            status = 3;
                        }else if(item.text == '发布'){
                            status = 4;
                        }else if(item.text == '完成'){
                            status = 5;
                        }else if(item.text == '暂停'){
                            status = 6;
                        }else if(item.text == '重启'){
                            status = 99;
                        }
                        if(projectList.treegrid('getSelections').length==0){
                            window.top.xhTop.showError('错误','请选择一个项目');
                            return false;
                        }
                        var ifChangeStatus = row['ifChangeStatus'];
                        if (ifChangeStatus==0) {
                            window.top.xhTop.showError('错误','请操作项目版本');
                            return false;
                        }
                        var iProject = projectList.treegrid('getSelections')[0];
                        // if(iProject['iProcess']<100&&status!=6&&status!=99&&status!=2){
                        //     window.top.xhTop.showError('错误','还有未完成的任务','error');
                        //     return false;
                        // }
                        $.ajax({
                            url:moduleUrl+'project/changeStatus',
                            data:{iProjectId:iProject['iProjectId'],iProjectStatus:status},
                            type:'POST',
                            success:function (data) {
                                window.top.xhTop.showSuccess(
                                    data.msg
                                );
                                projectList.treegrid('reload');
                            },
                            error:function (data) {
                                window.top.xhTop.showError(
                                    data.responseJSON.error,
                                    data.responseJSON.msg
                                );
                            }
                        });
                    }
                })
            }
        },
        onDblClickRow:function(row){
            $.ajax({
                url:moduleUrl+'task/checkTaskListPower',
                data:{iProjectId:row['iProjectId']},
                type:'POST',
                success:function (data) {
                    var title = '项目 - '+row['sProjectName'];
                    window.top.xhTop.createNewTab(title,moduleUrl+'task/index?iProjectId='+row['iProjectId'], true);
                },
                error:function (data) {
                    window.top.xhTop.showError(
                        data.responseJSON.error,
                        data.responseJSON.msg
                    );
                }
            });
        },
        onBeforeSelect:function (row) {
            $btn_menu=$('#bt_list_state').menubutton({
                iconCls: 'fa fa-bars',
                menu: '',
            });
            if(currentProjectId==0){
                return true;
            }else {
                if(currentProjectId==row['iProjectId']){
                    projectList.treegrid('unselect',currentProjectId);
                    projectList.treegrid('collapse',currentProjectId);
                    currentProjectId=0;
                    $('#bt_list_edit').setButtonDisable('eu');
                    $('#bt_list_add').setButtonEnable('eu');
                    $('#bt_list_delete').setButtonDisable('eu');
                    $('#bt_list_check').setButtonDisable('eu');
                    $('#bt_list_archive').setButtonDisable('eu');
                    return false;
                }
            }
        },
        onBeforeCheck:function (row) {
            $btn_menu=$('#bt_list_state').menubutton({
                iconCls: 'fa fa-bars',
                menu: '',
            });
            if(currentProjectId==0){
                return true;
            }else {
                if(currentProjectId==row['iProjectId']){
                    projectList.treegrid('uncheckRow',currentProjectId);
                    currentProjectId=0;
                    $('#bt_list_edit').setButtonDisable('eu');
                    $('#bt_list_add').setButtonEnable('eu');
                    $('#bt_list_delete').setButtonDisable('eu');
                    $('#bt_list_check').setButtonDisable('eu');
                    $('#bt_list_archive').setButtonDisable('eu');
                    return false;
                }
            }
        }
    });

    $('#bt_list_search').click(function () {
        var status = $('#prostatus').val();
        var bArchived = $('#bArchived').val();
        var projectName = $('#projectName').getElementValue();//获取input值
        projectList.treegrid('load',{status:status,projectName:projectName,bArchived:bArchived});
    });

    $('#proList_toolbar a').click(function () {
        var eleid = $(this).attr('id');
        var obj=projectList.treegrid('getSelections')[0];

        if(eleid =='bt_list_add'){
            if(xhExtra.buttonDisabled($(this))){
                return false;
            }
            window.top.xhTop.showModal({
                title:'新增项目',
                content: '<iframe src="'+moduleUrl+'project/projectForm?iProjectGrade=1"  style="width: 100%;;border-style: none;padding: 0px;height: 350px"  scrolling="yes"   ></iframe>',
                button:'提交',
                backClose: false
            });
        } else if(eleid =='bt_list_delete'&&obj!=undefined){
            if(xhExtra.buttonDisabled($(this))){
                return false;
            }
            myWindow.initPopUp({title: '确认删除', text: '确定要删除该项目吗?'},function () {
                $.ajax({
                    url:moduleUrl+'project/delProjectById',
                    data:{iProjectId:obj['iProjectId']},
                    type:'POST',
                    success:function (data) {
                        window.top.xhTop.showSuccess(data.msg);
                        $('#bt_list_edit').setButtonDisable('eu');
                        $('#bt_list_delete').setButtonDisable('eu');
                        $('#bt_list_check').setButtonDisable('eu');
                        $('#bt_list_archive').setButtonDisable('eu');
                        $('#bt_list_add').setButtonEnable('eu');
                        projectList.treegrid('reload');
                    },
                    error:function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })
        } else if(eleid =='bt_list_archive'&&obj!=undefined){
            if(xhExtra.buttonDisabled($(this))){
                return false;
            }
            myWindow.initPopUp({title: '归档确认', text: '确定要归档吗?'},function () {
                $.ajax({
                    url:moduleUrl+'project/archiveProjectById',
                    data:{iProjectId:obj['iProjectId']},
                    type:'POST',
                    success:function (data) {
                        window.top.xhTop.showSuccess(data.msg);
                        $('#bt_list_edit').setButtonDisable('eu');
                        $('#bt_list_delete').setButtonDisable('eu');
                        $('#bt_list_check').setButtonDisable('eu');
                        $('#bt_list_archive').setButtonDisable('eu');
                        $('#bt_list_add').setButtonEnable('eu');
                        projectList.treegrid('reload');
                    },
                    error:function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })
        } else if(eleid =='bt_list_check'&&obj!=undefined){
            if(xhExtra.buttonDisabled($(this))){
                return false;
            }
            myWindow.initPopUp({title: '审核确认', text: '确认要审核该项目及其版本与模块吗?'},function () {
                $.ajax({
                    url:moduleUrl+'project/checkProjectById',
                    data:{iProjectId:obj['iProjectId']},
                    type:'POST',
                    success:function (data) {
                        window.top.xhTop.showSuccess(
                            data.msg
                        );
                        $('#bt_list_edit').setButtonDisable('eu');
                        $('#bt_list_delete').setButtonDisable('eu');
                        $('#bt_list_check').setButtonDisable('eu');
                        $('#bt_list_archive').setButtonDisable('eu');
                        $('#bt_list_add').setButtonEnable('eu');
                        projectList.treegrid('reload');
                    },
                    error:function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })

        }
    });

    $('#bt_list_edit').click(function () {  //编辑
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var obj=projectList.treegrid('getSelections')[0];
        window.top.xhTop.showModal({
            title:'编辑项目',
            content: '<iframe src="'+moduleUrl+'project/projectForm?iProjectId='+obj['iProjectId']+'&iProjectGrade='+obj['iProjectGrade']+'&iParentPid='+obj['iParentPid']+'"  style="width: 100%;border-style: none;padding: 0px;height: 350px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose: false,
        });
    });

});

